import type { AxiosRequestConfig } from 'axios'
import request from './request'

// Generic type for API responses
export interface ApiResponse<T = any> {
  code: number
  data: T
  message: string
}

/**
 * HTTP request utility functions
 */
const http = {
  /**
   * GET request
   * @param url Request URL
   * @param params Query parameters
   * @param config Additional axios config
   */
  get<T = any>(url: string, params?: any, config?: AxiosRequestConfig): Promise<T> {
    return request({
      url,
      method: 'get',
      params,
      ...config
    })
  },

  /**
   * POST request
   * @param url Request URL
   * @param data Request body
   * @param config Additional axios config
   */
  post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
    return request({
      url,
      method: 'post',
      data,
      ...config
    })
  },

  /**
   * PUT request
   * @param url Request URL
   * @param data Request body
   * @param config Additional axios config
   */
  put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
    return request({
      url,
      method: 'put',
      data,
      ...config
    })
  },

  /**
   * DELETE request
   * @param url Request URL
   * @param params Query parameters
   * @param config Additional axios config
   */
  delete<T = any>(url: string, params?: any, config?: AxiosRequestConfig): Promise<T> {
    return request({
      url,
      method: 'delete',
      params,
      ...config
    })
  },

  /**
   * Upload file
   * @param url Request URL
   * @param formData FormData containing files
   * @param config Additional axios config
   */
  upload<T = any>(url: string, formData: FormData, config?: AxiosRequestConfig): Promise<T> {
    return request({
      url,
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      ...config
    })
  },

  /**
   * Download file
   * @param url Request URL
   * @param params Query parameters
   * @param config Additional axios config
   */
  download(url: string, params?: any, config?: AxiosRequestConfig): Promise<Blob> {
    return request({
      url,
      method: 'get',
      params,
      responseType: 'blob',
      ...config
    })
  }
}

export default http 